﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.MODEL.Statistics;
using MINERP.BLL.Statistics;
using MINERP.MODEL.FoundationModel;
using FastReport;

namespace MINERP.Statistics.ShopBill
{
    public partial class FrmOneShopAllBillDetail : Form
    {
        private List<MODEL.Statistics.Shop_Info> ListShop = new List<MINERP.MODEL.Statistics.Shop_Info>();    //商户集合 

        private BLL.Statistics.StatisticsBLL SSBLL = new MINERP.BLL.Statistics.StatisticsBLL();
        public FrmOneShopAllBillDetail()
        {
            InitializeComponent();
        }

        #region 单件
        //单件
        private static FrmOneShopAllBillDetail _instance;
        public static FrmOneShopAllBillDetail Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmOneShopAllBillDetail();
                }
                else if (_instance.IsDisposed)
                {
                    _instance = new FrmOneShopAllBillDetail();
                }
                return _instance;
            }
        }
        #endregion

        #region Load
        private void FrmOneShopAllBillDetail_Load(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            string nowStr = now.ToString("yyyy-MM-dd HH:mm");
            dtStartTime.Text = nowStr;
            dtEndTime.Text = nowStr;

            //加载商户
            GetShopList();
            BindShop();
        }
        #endregion

        #region 获取商户列表
        /// <summary>
        /// 获取商户列表
        /// </summary>
        private void GetShopList()
        {
            ListShop.Clear();
            ListShop = SSBLL.GetShopList();
        }
        #endregion

        #region 设置商户下拉列表
        /// <summary>
        /// 设置商户下拉列表
        /// </summary>
        private void BindShop()
        {
            if (cbShopID.Properties.Items != null)
            {
                cbShopID.Properties.Items.Clear();
            }
            foreach (MODEL.Statistics.Shop_Info s in ListShop)
            {
                cbShopID.Properties.Items.Add(s.ShopName);
            }
        }
        #endregion

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbShopID.Text == "--请选择商户--")
                {
                    return;
                }
                DateTime startTime = Convert.ToDateTime(dtStartTime.Text);
                DateTime endTime = Convert.ToDateTime(dtEndTime.Text);
                if (startTime >= endTime)
                    return;
                int shopId = 0;
                string shopName = cbShopID.Text;
                foreach (MINERP.MODEL.Statistics.Shop_Info s in ListShop)
                {
                    if (s.ShopName == shopName)
                    {
                        shopId = s.ShopID;
                        break;
                    }
                }
                if (shopId == 0)
                    return;

                //设置账单明细
                List<Bill_Info> ListBill = SSBLL.GetBillInfo(shopId, startTime, endTime);
                //获取商户账单明细(用List表示)
                List<MODEL.FoundationModel.SHOP_Bill_Info> ListShopBill = SSBLL.GetShop_Bill_InfoFromBill_Info(ListBill);
                //将账单从List转化成DataSet,并将对应的XML文件写入内存
                int status = 1;                                             //0未结单 1已结单(但这里不是原来的意思，这里是统计一段时间内该商户总共出了多少货，不管是否已结单)
                DataSet billSet = SSBLL.GetBillInfoFromListToDataSet(ListShopBill, status);
                report.Preview = this.previewControl1;
                report.Load(Application.StartupPath + "\\PrintShopBill2.frx");
                report.RegisterData(billSet);
                previewControl1.Refresh();
                previewControl1.Show();
                report.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void FrmOneShopAllBillDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance.Dispose();
            _instance = null;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textEdit1.Text))
            {
                StatisticsBLL sb = new StatisticsBLL();
                int billId = Convert.ToInt32(textEdit1.Text.Trim());
                SHOP_Bill_Info sbi = sb.GetBillToView(billId);

                //List<SHOP_Bill_Info> ListShopBill = new List<SHOP_Bill_Info>();
                //ListShopBill.Add(sbi);
                //int status = 1;                                             //0未结单 1已结单(但这里不是原来的意思，这里是统计一段时间内该商户总共出了多少货，不管是否已结单)
                //DataSet billSet = SSBLL.GetBillInfoFromListToDataSet(ListShopBill, status);
                //report.Preview = this.previewControl1;
                //report.Load(Application.StartupPath + "\\PrintShopBill2.frx");
                //report.RegisterData(billSet);
                //previewControl1.Refresh();
                //previewControl1.Show();
                //report.Show();
                PrintBill(sbi);
            }
        }
        public void PrintBill(SHOP_Bill_Info sbinfo)
        {
            #region 组织打印数据

            bool IsNeedPrintMoney = true;
            DataTable tableTitle = CreateShopBillTitle();
            DataRow titleRow = tableTitle.NewRow();
            titleRow["ShopName"] = sbinfo.Shop.ShopName;
            titleRow["Tel1"] = sbinfo.Shop.Tel1;
            titleRow["Tel2"] = sbinfo.Shop.Tel2;
            titleRow["ShopId"] = sbinfo.Shop.ShopId;
            titleRow["shopKeeper"] = sbinfo.Shop.ShopKeeper;
            titleRow["QQ"] = sbinfo.Shop.QQ;
            titleRow["BillId"] = sbinfo.BillId;
            titleRow["BillOrderCode"] = sbinfo.BillOrderCode;
            titleRow["CreateTime"] = sbinfo.CreateTime;
            titleRow["Adress"] = sbinfo.Shop.Address;
            titleRow["CheckOutType"] = sbinfo.CheckTypeString;
            titleRow["Remark"] = sbinfo.Shop.Remark;
            if (IsNeedPrintMoney)
            {
                titleRow["Balance"] = sbinfo.ShopBalance;
                titleRow["BillCount"] = sbinfo.BillCount;
            }
            else
            {
                titleRow["Balance"] = 0;
                titleRow["BillCount"] = 0;
            }


            titleRow["Salesman"] = sbinfo.Salesman;
            titleRow["CreditLevel"] = sbinfo.CreditLevel == null ? 0 : sbinfo.CreditLevel;
            tableTitle.Rows.Add(titleRow);

            tableTitle.TableName = "tableTitle";


            DataTable tableDetail = CreateShopBillDetail();
            DataTable tableSubDetail = CreateShopBillSubDetail();
            List<Bill_Detail_Info> detailList = sbinfo.List_Detail.ToList();

            foreach (Bill_Detail_Info billDetail in detailList)
            {
                DataRow row = tableDetail.NewRow();
                row["BillDetailId"] = billDetail.BillDetailId;
                row["OrderInfoCode"] = billDetail.OrderInfoCode;
                row["OrderProductName"] = billDetail.OrderProductName;
                row["CtmName"] = billDetail.CtmName;
                row["Remark"] = billDetail.Remark;

                tableDetail.Rows.Add(row);
                List<Bill_Detail_Detail_Info> subDetail = billDetail.ListDetail_Detail.ToList();
                foreach (Bill_Detail_Detail_Info detail in subDetail)
                {
                    DataRow rowSub = tableSubDetail.NewRow();
                    rowSub["DetailName"] = detail.DetailName;
                    rowSub["BillDetailId"] = billDetail.BillDetailId;
                    rowSub["Spec"] = detail.Spec;
                    rowSub["Num"] = detail.Num;
                    rowSub["UnitPrice"] = detail.UnitPrice;
                    if (IsNeedPrintMoney)
                    {
                        rowSub["Count"] = detail.Count;
                    }
                    else
                    {
                        rowSub["Count"] = 0;
                    }

                    rowSub["Area"] = detail.Area;
                    rowSub["Type"] = detail.Type;
                    rowSub["DetailRemark"] = detail.DetailRemark;
                    tableSubDetail.Rows.Add(rowSub);
                }
            }

            tableDetail.TableName = "tableDetail";
            tableSubDetail.TableName = "tableSubDetail";
            DataTable tableBottom = CreateFahuoTable();
            #endregion



            #region 打印报表
            DataSet set = new DataSet("ds");
            set.Tables.Add(tableTitle);
            set.Tables.Add(tableDetail);
            set.Tables.Add(tableSubDetail);
            set.Tables.Add(tableBottom);

            set.WriteXml(@"C:\Windows\xmldata\PrintShopBill.xml", XmlWriteMode.WriteSchema);
            Report report = new Report();
            report.Load(Application.StartupPath + "\\Report\\PrintShopBill.frx");
            report.RegisterData(tableTitle, "tableTitle");
            report.RegisterData(tableDetail, "tableDetail");
            report.RegisterData(tableSubDetail, "tableSubDetail");
            //report.RegisterData(tableBottom, "tableBottom");
            report.Preview = previewControl1;
            previewControl1.Refresh();
            previewControl1.Show();
            report.Show();
            #endregion


        }

        /// <summary>
        /// 创建title表
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopBillTitle()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ShopName", typeof(string));
            table.Columns.Add("Tel1", typeof(string));
            table.Columns.Add("Tel2", typeof(string));
            table.Columns.Add("ShopId", typeof(int));
            table.Columns.Add("BillOrderCode", typeof(string));
            table.Columns.Add("shopKeeper", typeof(string));
            table.Columns.Add("QQ", typeof(string));
            table.Columns.Add("BillId", typeof(string));
            table.Columns.Add("CreateTime", typeof(DateTime));
            table.Columns.Add("Balance", typeof(double));
            table.Columns.Add("BillCount", typeof(double));
            table.Columns.Add("CreditLevel", typeof(int));
            table.Columns.Add("Salesman", typeof(string));
            table.Columns.Add("Adress", typeof(string));
            table.Columns.Add("CheckOutType", typeof(string));
            table.Columns.Add("Remark", typeof(string));
            return table;

        }
        /// <summary>
        /// 创建明细产品
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopBillDetail()
        {
            DataTable table = new DataTable();
            table.Columns.Add("BillDetailId", typeof(int));
            table.Columns.Add("OrderInfoCode", typeof(string));
            table.Columns.Add("OrderProductName", typeof(string));
            table.Columns.Add("CtmName", typeof(string));
            table.Columns.Add("Remark", typeof(string));
            return table;
        }
        /// <summary>
        /// 创建明细收费项目表
        /// 
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopBillSubDetail()
        {
            DataTable table = new DataTable();
            table.Columns.Add("BillDetailId", typeof(int));
            table.Columns.Add("DetailName", typeof(string));
            table.Columns.Add("Spec", typeof(string));
            table.Columns.Add("Num", typeof(int));
            table.Columns.Add("UnitPrice", typeof(double));
            table.Columns.Add("Count", typeof(double));
            table.Columns.Add("Area", typeof(double));
            table.Columns.Add("Type", typeof(int));
            table.Columns.Add("DetailRemark", typeof(string));
            return table;
        }
        /// <summary>
        /// 创建发货信息表
        /// </summary>
        /// <returns></returns>
        private DataTable CreateFahuoTable()
        {
            DataTable table = new DataTable("tableBottom");
            table.Columns.Add("ConsigneeName", typeof(string));
            table.Columns.Add("ConsigneeTelePhone", typeof(string));
            table.Columns.Add("DistributionAddress", typeof(string));
            table.Columns.Add("BusTimetable", typeof(string));//收货单位
            table.Columns.Add("ReMark", typeof(string));
            table.Columns.Add("DistributionMode", typeof(string));
            table.Columns.Add("Fahuodanwei", typeof(string));
            table.Columns.Add("Fahuodianhua", typeof(string));

            return table;
        }
    }
}
